/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package br.ufes.cwtools.joinus.home.campaigns;

import br.ufes.cwtools.joinus.util.ServerCom;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import br.ufes.cwtools.joinus.R;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class UserChooser extends Activity {
	private ImageButton add;
	private LayoutInflater viewInflate;
	private ListView cont;
	private ListView commu;
	private SQLiteDatabase myBase;
	private long[] commukeys;
	private long[] contkeys;
	private String userName;

	public void onCreate(Bundle b) {
		super.onCreate(b);
		this.setContentView(R.layout.userchooser);
		viewInflate = this.getLayoutInflater();
		cont = (ListView) this.findViewById(R.id.userchooserind);
		commu = (ListView) this.findViewById(R.id.userchoosercom);

		Bundle fromCaller = this.getIntent().getExtras();
		this.userName = fromCaller.getString("username");

		myBase = this.openOrCreateDatabase("homedb", MODE_PRIVATE, null);
		add = (ImageButton) this.findViewById(R.id.userchooser_add);
		LoadCommuData();
		LoadContData();

		add.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				Bundle b = new Bundle();
				int c = 0;
				for (int n = 0; n < cont.getAdapter().getCount(); n++) {
					RelativeLayout r = (RelativeLayout) cont.getAdapter().getView(n, null, null);
					CheckBox cb = (CheckBox) r.findViewById(R.id.line_template_cb);
					if (cb.isChecked()) {
						Log.d("CONT", contkeys[n] + "");
						b.putLong("cont" + c, contkeys[n]);
						c++;
					}
				}
				b.putInt("ncont", c);  // Guarda no bundle o total de contatos selecionados.

				c = 0;
				for (int n = 0; n < commu.getAdapter().getCount(); n++) {
					RelativeLayout r = (RelativeLayout) commu.getAdapter().getView(n, null, null);
					CheckBox cb = (CheckBox) r.findViewById(R.id.line_template_cb);
					if (cb.isChecked()) {
						Log.d("COMMU", commukeys[n] + "");
						b.putLong("comm" + c, commukeys[n]);
						c++;
					}
				}
				b.putInt("ncomm", c);  // Guarda no bundle o total de comunidades selecionadas.
				
				Intent data = new Intent();
				data.putExtras(b);
				setResult(RESULT_OK, data);
				finish();
			}
		});
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		myBase.close();
	}
	
	private void LoadContData() {
		Cursor query = myBase.query("contacts", null, "user='" + this.userName
				+ "'", null, null, null, null);
		String[][] array = new String[query.getCount()][];
		contkeys = new long[query.getCount()];
		for (int i = 0; i < query.getCount(); i++) {
			query.moveToPosition(i);
			array[i] = new String[] { query.getString(1),
					"User id: " + query.getString(0) };
			contkeys[i] = query.getLong(0);
		}
		
		query.close();
		
		add(array, cont);
	}

	private void LoadCommuData() {
		Cursor query = myBase.query("communities", null, "user='"
				+ this.userName + "'", null, null, null, null);
		String[][] array = new String[query.getCount()][];
		commukeys = new long[query.getCount()];
		for (int i = 0; i < query.getCount(); i++) {
			query.moveToPosition(i);
			array[i] = new String[] {
					query.getString(1),
					"Owner: " + ServerCom.getUserName(query.getString(2))
					+ " (" + query.getString(2) + ")" };
			commukeys[i] = query.getLong(0);
		}
		
		query.close();
		
		add(array, commu);
	}

	public void add(String[][] toAdd, ListView v) {
		RelativeLayout[] rl = new RelativeLayout[toAdd.length];
		for (int i = 0; i < toAdd.length; i++) {
			RelativeLayout r = (RelativeLayout) viewInflate.inflate(R.layout.line_template, null);
			((TextView) r.getChildAt(1)).setText(toAdd[i][0]);
			((TextView) r.getChildAt(2)).setText(toAdd[i][1]);
			rl[i] = r;
		}
		v.setAdapter(new MyListAdapter(rl));
	}

	final class MyListAdapter extends BaseAdapter {
		RelativeLayout[] item;

		public MyListAdapter(RelativeLayout[] v) {
			item = v;
		}

		public Object getItem(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			return item[position];
		}

		public long getItemId(int position) {
			return position;
		}

		public int getCount() {
			return item.length;
		}
	}
}
